/*
 * @(#)XfjDialog.java        1.0 2000/05/09
 *
 * This file is part of Xfuzzy 3.0, a design environment for fuzzy logic
 * based systems.
 *
 * (c) 2000 IMSE-CNM. The authors may be contacted by the email address:
 *                    xfuzzy-team@imse.cnm.es
 *
 * Xfuzzy is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * Xfuzzy is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
//		DIALOGO DEL COMPILADOR A JAVA			//
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//

package xfuzzy.xfj;

import xfuzzy.*;
import xfuzzy.lang.*;
import xfuzzy.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class XfjDialog extends JDialog implements ActionListener,
WindowListener {

	/**
	 * C�digo asociado a la clase serializable
	 */
	private static final long serialVersionUID = 95505666603044L;

 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
 //			MIEMBROS PRIVADOS			//
 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//

 private Specification spec;
 private File dir;
 private String pkgname;
 private XTextForm text[];
 private String msg;

 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
 //			   CONSTRUCTORES			//
 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//

 //-------------------------------------------------------------//
 // Constructor utilizado en la ventana principal de Xfuzzy	//
 //-------------------------------------------------------------//

 public XfjDialog(Xfuzzy xfuzzy,Specification spec) {
  super(xfuzzy.frame,"Xfj", true);
  this.spec = spec;
  this.dir = spec.getFile().getParentFile();

  String lb[] = {"Compile","Cancel"};
  XCommandForm form = new XCommandForm(lb,lb,this);
  form.setCommandWidth(120);
  form.block();

  text = new XTextForm[2];
  text[0] = new XTextForm("Package");
  text[0].setLabelWidth(100);
  text[0].setFieldWidth(400);
  text[0].setAlignment(JLabel.CENTER);

  text[1] = new XTextForm("Path",this);
  text[1].setActionCommand("Browse");
  text[1].setLabelWidth(100);
  text[1].setFieldWidth(400);
  text[1].setAlignment(JLabel.CENTER);
  text[1].setEditable(false);
  text[1].setText(dir.getAbsolutePath());
  XTextForm.setWidth(text);

  Container content = getContentPane();
  content.setLayout(new BoxLayout(content,BoxLayout.Y_AXIS));
  content.add(new XLabel("Java Compiler"));
  content.add(text[0]);
  content.add(text[1]);
  content.add(form);

  setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
  addWindowListener(this);
  setFont(XConstants.font);
  setLocationRelativeTo(xfuzzy.frame);
  pack();
 }

 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
 //			METODOS PUBLICOS			//
 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//

 //-------------------------------------------------------------//
 // Descripcion del resultado de la compilacion			//
 //-------------------------------------------------------------//

 public String getMessage() {
  return msg;
 }

 //-------------------------------------------------------------//
 // Interfaz ActionListener					//
 //-------------------------------------------------------------//

 public void actionPerformed(ActionEvent e) {
  String command = e.getActionCommand();
  if(command.equals("Browse")) browse();
  else if(command.equals("Compile")) generate();
  else if(command.equals("Cancel")) cancel();
 }

 //-------------------------------------------------------------//
 // Interfaz WindowListener					//
 //-------------------------------------------------------------//

 public void windowOpened(WindowEvent e) {}
 public void windowClosing(WindowEvent e) { cancel(); }
 public void windowClosed(WindowEvent e) {}
 public void windowIconified(WindowEvent e) {}
 public void windowDeiconified(WindowEvent e) {}
 public void windowActivated(WindowEvent e) {}
 public void windowDeactivated(WindowEvent e) {}

 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
 //			METODOS PRIVADOS			//
 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//

 //-------------------------------------------------------------//
 // Genera el codigo java					//
 //-------------------------------------------------------------//

 private void generate() {
  pkgname = text[0].getText();
  Xfj compiler = new Xfj(spec,dir,pkgname);
  this.msg = compiler.getMessage();
  setVisible(false);
 }

 //-------------------------------------------------------------//
 // Selecciona el directorio de destino				//
 //-------------------------------------------------------------//

 private void browse() {
  XFileChooser chooser = new XFileChooser(dir,XFileChooser.SELECT);
  chooser.setTitle("Target directory");
  chooser.addNoneFilter();
  if(chooser.showDialog() != XFileChooser.SELECTED) return;
  dir = chooser.getSelectedFile();
  text[1].setText(dir.getAbsolutePath());
 }

 //-------------------------------------------------------------//
 // Cierra la ventana sin generar el codigo			//
 //-------------------------------------------------------------//

 private void cancel() {
  setVisible(false);
 }
}
